<template>
  <div>

    <el-button style="position: absolute; right: 26px; top:55px;" type="primary" size="mini" @click="kuFang()" round>
      库房增加</el-button>

    <el-container
      style="height: 525px; overflow: hidden; background: white; border:1px solid white ; border-radius: 8px; margin-top: 10px;">
      <div style="margin-top:10px;">
        <template>
          <el-table :data="tableData" style="width: 100%; height: 426px;  display: block;" size="medium">
            <el-table-column prop="repid" label="库存编号" width="350">
            </el-table-column>
            <el-table-column prop="houseName" label="库房名称" width="180">
            </el-table-column>
            <el-table-column prop="resperson" label="责任人编号" width="170">
            </el-table-column>
            <el-table-column prop="name" label="变更" width="170">
              <template slot-scope="scope">
                <el-button type="info" size="mini" @click="handleUdate(scope.row)" round>更改</el-button>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-popconfirm confirm-button-text='好的' cancel-button-text='不用了' icon="el-icon-info" icon-color="red"
                  title="确定释放该库房？"  @confirm="handleDelete(scope.row.repid)">
                  <el-button   slot="reference"  type="danger" size="mini"  round>释放</el-button>
                </el-popconfirm>
              </template>



            </el-table-column>
          </el-table>
          <div class="block" style="display: block; margin-top: 15px;">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
              :page-sizes="[2,4,6,7]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
              :total="total">
            </el-pagination>
          </div>
        </template>
      </div>
    </el-container>

    <el-dialog title="添加" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="库房编号" :label-width="formLabelWidth" size="mini" style="color: blue;">
          <el-input v-model="form.idkey" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="库房名称" :label-width="formLabelWidth" size="medium">
          <el-input v-model="form.houseName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="责任人编号" :label-width="formLabelWidth" size="medium">
          <el-input v-model="form.resperson" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button style="width: 45px ; height: 30px;" @click="dialogFormVisible = false">取 消</el-button>
        <el-button style="width: 45px ; height: 30px;" type="primary" @click="addKuFang()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改" :visible.sync="dialogUpdateVisible">
      <el-form :model="form">
        <el-form-item label="库房编号" :label-width="formLabelWidth" size="mini" style="color: blue;">
          <el-input v-model="form.repid" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="库房名称" :label-width="formLabelWidth" size="medium">
          <el-input v-model="form.houseName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="责任人编号" :label-width="formLabelWidth" size="medium">
          <el-input v-model="form.resperson" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button style="width: 45px ; height: 30px;" @click="dialogUpdateVisible = false">取 消</el-button>
        <el-button style="width: 45px ; height: 30px;" type="primary" @click="update()">确 定</el-button>
      </div>
    </el-dialog>





  </div>
</template>

<script>
  export default {
    name: 'repattr',
    data() {
      return {
        tableData: [],
        pageNum: 1,
        pageSize: 7,
        total: 0,
        dialogUpdateVisible: false,
        dialogFormVisible: false,
        form: {
          repid: '',
          houseName: '',
          resperson: '',
          idkey: 0
        },
        formLabelWidth: '120px'
      }
    },
    methods: {
      tableRowClassName({
        row,
        rowIndex
      }) {
        if (rowIndex === 1) {
          return 'warning-row';
        } else if (rowIndex === 3) {
          return 'success-row';
        }
        return '';
      },
      handleSizeChange(val) {
        this.pageSize = val;
        this.GetOnline(this.pageNum, this.pageSize);
      },
      handleCurrentChange(val) {
        this.pageNum = val;
        this.GetOnline(this.pageNum, this.pageSize);
      },
      GetOnline(pageNum, pageSize) {
        var data = new URLSearchParams();
        data.append("pageNum", pageNum);
        data.append("pageSize", pageSize);
        this.$http.post('rep/getRep', data).then(res => {
          this.pageNum = res.data.pageNum;
          this.pageSize = res.data.pageSize;
          this.total = res.data.total;
          this.tableData = res.data.rows;
        }).catch(err => {
          console.log(err);
        })
      },
      addKuFang() {
        var data = this.form;
        this.$http.post('rep/addKufang', this.$qs.stringify(data)).then(res => {
          if (res.status === 200) {
            this.$message({
              message: '添加成功',
              type: 'success'
            });
            this.dialogFormVisible = false;
          }
        }).catch(err => {
          this.$message.error('责任人编号有误!');
        })
      },
      kuFang() {
        this.$http.post('rep/idKey?tableName=rep_house').then(res => {
          if (res.status == 200) {
            this.form.idkey = res.data;
            this.GetOnline(this.pageNum, this.pageSize);
            this.dialogFormVisible = true
          }
        }).catch(err => {
          console.log(err);
        })
      },
      handleUdate(data) {

        this.form.repid = data.repid;
        this.form.houseName = data.houseName;
        this.form.resperson = data.resperson;
        this.dialogUpdateVisible = true;
      },
      update() {
        var data = this.form;
        this.$http.post('rep/updKufang', this.$qs.stringify(data)).then(res => {
          if (res.status === 200) {
            this.$message({
              message: '修改成功',
              type: 'success'
            });
            this.form.repid = 0;
            this.form.houseName = '';
            this.form.resperson = '';
            this.GetOnline(this.pageNum, this.pageSize);
            this.dialogUpdateVisible = false;
          }
        }).catch(err => {
          this.$message.error('责任人编号有误!');
        })

      },
      handleDelete(data){
        //repositoryService
        var repid =data;
        this.$http.post('rep/delKufang?repid='+repid).then(res => {
          if (res.status === 200) {
            this.$message({
              message: '完成',
              type: 'success'
            });
            this.GetOnline(this.pageNum, this.pageSize);
          }
        }).catch(err => {
          console.log(err);
        })
      }
    },
    mounted() {
      this.GetOnline(this.pageNum, this.pageSize);
    }
  }
</script>

<style scoped="scoped">
  * {
    padding: 0px;
    margin: 0px;
  }

  .el-breadcrumb__inner {
    color: black;

  }

  .el-form-item {
    margin-top: 15px;
  }

  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }

  .el-breadcrumb__item:last-child .el-breadcrumb__inner,
  .el-breadcrumb__item:last-child .el-breadcrumb__inner a,
  .el-breadcrumb__item:last-child .el-breadcrumb__inner a:hover,
  .el-breadcrumb__item:last-child .el-breadcrumb__inner:hover {
    font-weight: 400;
    color: black;
    cursor: text;
  }
</style>
